// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import App from "./App";
import router from "./router";
import store from "./Store";
import Toast from "./components/Common/Toast/index";
import vCharts from "v-charts";
import { getCookie } from "./Tools/cookie";

Vue.config.productionTip = false;
Vue.use(vCharts);

Vue.use(Toast, {
  time: 2000
});

router.beforeEach(async (to, from, next) => {
  switch (to.path) {
    case "/":
      next();
      break;
    case "/Sign":
      next();
      break;
    case "/Password":
      next();
      break;
    default:
      // 检查Cookie 和 登录状态
      if (getCookie("userName")) {
        // 如果来自登录
        if (from.path == "/") {
          // 尝试与服务器验证
          await store.dispatch("vaildLogin");
        }
        // 获取登录状态
        if (store.getters.getLogin) {
          // 跳转
          next();
          // 跳出 switch
          break;
        }
      }
      next({
        // 跳转到登录页面
        path: "/",
        query: {
          // 传入当前页面，方便登录后直接跳转
          path: to.fullPath
        }
      });
      break;
  }
});
/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  store,
  components: { App },
  template: "<App/>"
});
